package org.betasoftware.cloud.drive;

import java.io.IOException;

import org.betasoftware.cloud.CloudFile;
import org.betasoftware.cloud.CloudProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.model.File;
import com.google.api.services.drive.model.FileList;

public class DriveCloudProvider implements CloudProvider {

	private Drive drive;
	private String applicationName;

	private HttpTransport httpTransport;
	private JsonFactory jsonFactory;

	public DriveCloudProvider(String applicationName) {
		this.applicationName = applicationName;
	}

	public void authorize(Credential credential) {
		drive = new Drive.Builder(httpTransport, jsonFactory, credential)
				.setApplicationName(applicationName).build();
	}

	public void setHttpTransport(HttpTransport httpTransport) {
		this.httpTransport = httpTransport;
	}
	
	public void setJsonFactory(JsonFactory jsonFactory) {
		this.jsonFactory = jsonFactory;
	}
	
	public String getPrefix() {		
		return "drive://";
	}
	
	public CloudFile createOrRetrieve(String filename) {
		try {
			FileList list = drive.files().list().execute();
			for (File file : list.getItems()) {
				logger.info(file.getTitle());
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	private static final Logger logger = LoggerFactory.getLogger(DriveCloudProvider.class);
}
